package com.mr.cai.main.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.mr.cai.main.fragment.BaseFragment;
import com.mr.cai.main.fragment.ClassifyContentFragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-2-25.
 */
public class ClassifyFgContentAdapter extends FragmentStatePagerAdapter {
    private SparseArray<WeakReference<Fragment>> fragments = new SparseArray<>();

    private List objects=new ArrayList(); //从外部传入的参数集合

    public ClassifyFgContentAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setList(List objects){
        this.objects = objects;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        BaseFragment fragment=ClassifyContentFragment.newInstance();
        return fragment;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return super.isViewFromObject(view, object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ClassifyContentFragment fragment= (ClassifyContentFragment) super.instantiateItem(container, position);
        WeakReference<Fragment> weakReference=new WeakReference<Fragment>(fragment);
        fragments.put(position,weakReference);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        fragments.remove(position);
        super.destroyItem(container, position, object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void remove(int position){
        objects.remove(position);
        notifyDataSetChanged();
    }

    public BaseFragment getCurrentFragment(int position){
        return (BaseFragment) fragments.get(position).get();
    }
    public void clearData(){}
    @Override
    public int getCount() {
        return objects.size();
    }
}
